<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%@ include file="/common/taglibs.jsp"%>
<%@include file="/common/common-header.jsp" %>
<script type="text/javascript">
	$(function(){
	
		$(".addFilter").each(function(){
			var id = $(this).attr("id");
			$(this).click(function(){
				var expressionIdx = id.replace(/\D/g,"");
				var filterIdx = $(".table_"+id).length;
				var appendHtml = $("#filterContent").html().replace(/(.*)@expressionIdx#(.*)/g,"$1"+expressionIdx.toString()+"$2").replace(/(.*)#expressionIdx@(.*)/g,"$1["+expressionIdx.toString()+"]$2").replace(/(.*)@filterIdx@(.*)/g,"$1["+filterIdx.toString()+"]$2").replace(/(.*)@#filterIdx#@(.*)/g,"$1"+filterIdx.toString()+"$2").replace(/(.*)@#filterIdx1#@(.*)/g,"$1"+(filterIdx + 1).toString()+"$2");
				
				$("td."+id).append(appendHtml);
				return false;
			});
		});
		
		$("#template").change(function(){
			var template=$("#template").val();
			location.href = "${ctx}/crawl-api-object/create?template="+template;
		});
		
		$("#addParam").click(function(){
			var paramIdx = $(".param_table").length;
			paramIdx = (paramIdx-1) < 0 ? 0:(paramIdx-1);
			var appendHtml = $("#paramContent").html().replace(/(.*)#paramIdx@(.*)/g,"$1["+paramIdx.toString()+"]$2").replace(/(.*)#param-Idx@(.*)/g,"$1"+(paramIdx+1).toString()+"$2").replace(/(.*)#paramIdx#(.*)/g,"$1"+paramIdx.toString()+"$2");
			$("#params_table").append(appendHtml);
			return false;
		});
		// classify
		//$("select[id^='select_id']").change(function(){
		//	var _this=this;
		//	var temp= $(this).children('option:selected').val();
		//	$.ajax({
		//	   url:"${ctx}/crawl-api-object/"+temp+"/getchild",
		//	   dataType:"json",
		//	   success: function(data){
		//		   var html="";
		//		   for(var i=0;i<data.length;i++)
		//			   html+="<option value ='"+data[i].id+"'>"+data[i].name+"</option>";
		//		var className=$(_this).attr("id")+"_item";
		//	    $("#"+className).html(html);
		//	    if($("#select_id_item_item").val()){
		//			$("#select_id_item_item option:selected").val($("#select_id_item_item option:selected").text());
		//		}
		//	   }
		//	});
	   // });
		

		//if($('input[name="_method"]').val()=="put"){
			//var html1 ='<option  value=${firstchannel.id} >${firstchannel.name}</option>';
		//	var html2 ='<option  value=${secondchannel.id} >${secondchannel.name}</option>';
		//	var html3 ='<option  value=${thirdchannel.name} >${thirdchannel.name}</option>';
		//	$("#select_id option").each(
		//			function(){
		//				if($(this).text()=="${firstchannel.name}"){
		//				$(this).attr("selected",true);
		//		      }
		//	   });
		//	$("#select_id_item").html(html2);
		//	$("#select_id_item_item").html(html3);
			
		//}
	});
</script>
</head>
<body>
<form:form method="post" modelAttribute="crawlApiObject">
<input type="hidden" name="_method" value="${_method}"></input>
 <!--content start-->
<div class="content">
<div class="table">
<div class="contentNav"><h1><span class="allIco2 ico_6"></span>采集规则录入</h1></div>
 <div class="tips"><img src="${ctx}/resources/images/tips.gif" align="left" />所有带有<span class="red">*</span>为必填项</div>
<div class="info border">

<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tbody>
<tr class="table_con"> 
 <td width="240" align="right" nowrap="nowrap">选择模板：</td>
 <td colspan="2"> 
	<select  id="template">
	<c:forEach var="template" items="${templates}">
   <option value="${template.key}" <c:if test="${template.key==templateInt}"> selected</c:if>> <c:out  value="${template.value}"/></option>
  </c:forEach>
	</select>
</td> 
 </tr> 
<tr class="table_con">
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>采集名称：</td>
<td colspan="2">
	<form:input path="name" cssClass="input5 fontMar"/>(注. 请注明采集站点和采集内容,以“-”隔开 eg.淘宝网-xxxxxx)
</td>
</tr>
<tr class="table_con">
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>入口地址：</td>
<td colspan="2">
	<form:input path="enterUrl" cssClass="input5 fontMar"/>
</td>
</tr>
<tr class="table_con">
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>列表表达式：</td>
<td colspan="2">
	<form:input path="itemsExpression" cssClass="input5 fontMar"/>
</td>
</tr>
<tr class="table_con">
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>提交地址：</td>
<td colspan="2">
	<form:input path="callbackUrl" cssClass="input5 fontMar"/>
</td>
</tr>
<tr class="table_con">
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>所属项目：</td>
<td colspan="2">
	<c:if test="${!empty crawlApiObject.project}"><form:input path="project" cssClass="input5 fontMar" value="${crawlApiObject.project}" /></c:if>
	<c:if test="${empty crawlApiObject.project}"><form:input path="project" cssClass="input5 fontMar"/></c:if>(eg. com.xinziruo.mobile)
</td>
</tr>

<!-- <tr class="table_con">
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>采集分类：</td>
<td colspan="2">
	<select  id="select_id">
		<c:forEach items="${channels }" var="channel">
			<option label="${channel.name}" value="${channel.id}">${channel.name}</option>
		</c:forEach>
	</select> 
	<select name="childrenpath" id="select_id_item"  ></select>
	<form:select name="grandchildpath" id="select_id_item_item" path="classify"  value=""></form:select> 
</td>
</tr> -->
<tr class="table_con">
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>所属分类：</td>
<td colspan="2">
	<c:if test="${!empty crawlApiObject.classify}"><form:input path="classify" cssClass="input5 fontMar" value="${crawlApiObject.classify}" /></c:if>
	<c:if test="${empty crawlApiObject.classify}"><form:input path="classify" cssClass="input5 fontMar"/></c:if>
 </td>
</tr>
<tr class="table_con">
<td width="240" align="right" nowrap="nowrap">单页抓取：</td>
<td colspan="2">
	<form:select path="followingItem">
		<form:option value="true">是</form:option>
		<form:option value="false">否</form:option>
	</form:select>
</td>
</tr>
<c:forEach items="${ crawlApiObject.parseExpressions }" var="expr" varStatus="status">
<tr class="table_con">
<form:hidden path="parseExpressions[${status.index}].api.id" />
<form:hidden path="parseExpressions[${status.index}].id" />
<form:hidden path="parseExpressions[${status.index}].name" />
<form:hidden path="parseExpressions[${status.index}].sort" />
<form:hidden path="parseExpressions[${status.index}].descr" />
<td width="240" align="right" nowrap="nowrap" valign="top">${expr.descr}：</td>
<td colspan="2" class="parseExpressions${status.index}">
	<form:input path="parseExpressions[${status.index}].expression" cssClass="input5 fontMar"/>
	<form:select path="parseExpressions[${status.index}].resultType" >
		<form:option value="text">文本</form:option>
		<form:option value="link">链接</form:option>
	</form:select>
	<a href="#" class="addFilter" id="parseExpressions${status.index}">添加文本过滤器</a>
	<c:forEach items="${expr.stringFilterConfigs }" var="filter" varStatus="filterIdx">
	<table cellpadding="0" cellspacing="0" class="t_k table_parseExpressions${status.index}" id="parseExpressions${status.index}.stringFilterConfigs${filterIdx.index }">
	<form:hidden path="parseExpressions[${status.index}].stringFilterConfigs[${filterIdx.index }].id" />
	<form:hidden path="parseExpressions[${status.index}].stringFilterConfigs[${filterIdx.index }].sort" />
	<tbody>
	<tr class="table_con">
	<td colspan="2" align="center">文本过滤器${filterIdx.index + 1}</td></tr>
	<tr class="table_con">
	<td width="25%">正则表达式：</td>
	<td>
	<form:input path="parseExpressions[${status.index}].stringFilterConfigs[${filterIdx.index }].regex" class="n_i"/>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">替换表达式：</td>
	<td><form:input path="parseExpressions[${status.index}].stringFilterConfigs[${filterIdx.index }].replacement" class="n_i"/></td>
	</tr>
	</tbody>
	</table>
	</c:forEach>
</td>
</tr>
</c:forEach>
<tr>
<td width="240" align="right" valign="top" nowrap="nowrap">参数负载：</td>
<td colspan="2" id="params_table">
	<form:textarea path="payloadString" cssClass="input5 fontMar input_0"/>
	<a href="#" id="addParam" >添加参数</a>
	<c:forEach items="${crawlApiObject.params }" var="param" varStatus="status">
	<form:hidden path="params[${status.index}].api.id" />
	<form:hidden path="params[${status.index}].id" />
	<form:hidden path="params[${status.index}].sort" />
	<table cellpadding="0" cellspacing="0" class="t_k param_table" id="param${status.index}">
	<tbody>
	<tr class="table_con">
	<td colspan="2" align="center">参数${status.index + 1}</td></tr>
	<tr class="table_con">
	<td width="25%">名称：</td>
	<td>
	<form:input path="params[${status.index}].name" class="n_i table"/>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">值：</td>
	<td><form:input path="params[${status.index}].value" class="n_i"/></td>
	</tr>
	<tr class="table_con">
	<td width="25%">编码：</td>
	<td>
	<form:select path="params[${status.index }].encode" class="n_i">
		<form:option value="GBK">gbk</form:option>
		<form:option value="UTF-8">utf-8</form:option>
	</form:select>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">负载攻击：</td>
	<td>
	<form:select path="params[${status.index }].attack" class="n_i">
		<form:option value="true">是</form:option>
		<form:option value="false">否</form:option>
	</form:select>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">类型：</td>
	<td>
	<form:select path="params[${status.index }].type" class="n_i">
		<form:option value="URL">url</form:option>
		<form:option value="PARAM">param</form:option>
		<form:option value="COOKIE">cookie</form:option>
		<form:option value="BODY">body</form:option>
	</form:select>
	</td>
	</tr>
	</tbody>
	</table>
	</c:forEach>
</td>
</tr>
<tr>
<td width="240" align="right" nowrap="nowrap"><span class="red">*</span>采集任务cron表达式：</td>
<td colspan="2">
	<form:input path="cronExpression" cssClass="input5 fontMar" value=""/>
</td>
</tr>
</tbody>
</table>
</div>
<div class="contactBbutton">
<input id="ok" type="submit" value="提交" class="button1" />&nbsp;
<input id=“back” type="button" value="返回" class="button1" onclick="javascript:history.go(-1);"/>
</div>
  
</div></div>
</form:form>

<div id="filterContent" style="display: none;">
	<table cellpadding="0" cellspacing="0" class="t_k table_parseExpressions@expressionIdx#">
	<tbody>
	<tr class="table_con">
	<td colspan="2" align="center">文本过滤器@#filterIdx1#@</td></tr>
	<tr class="table_con">
	<td width="25%">正则表达式：</td>
	<td>
	<input name="parseExpressions#expressionIdx@.stringFilterConfigs@filterIdx@.sort" type="hidden" value="@#filterIdx#@"/>
	<input name="parseExpressions#expressionIdx@.stringFilterConfigs@filterIdx@.regex" class="n_i" type="text" value=""/>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">替换表达式：</td>
	<td>
	<input name="parseExpressions#expressionIdx@.stringFilterConfigs@filterIdx@.replacement" class="n_i" type="text" value=""/>
	</td>
	</tr>
	</tbody>
	</table>
</div>
<div id="paramContent" style="display: none;">
	<table cellpadding="0" cellspacing="0" class="t_k param_table">
	<tbody>
	<tr class="table_con">
	<td colspan="2" align="center">参数#param-Idx@</td></tr>
	<tr class="table_con">
	<td width="25%">名称：</td>
	<td>
	<input name="params#paramIdx@.sort" type="hidden" value="#paramIdx#"/>
	<input name="params#paramIdx@.name" class="n_i" type="text" value=""/>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">值：</td>
	<td>
	<input name="params#paramIdx@.value" class="n_i" type="text" value=""/>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">编码：</td>
	<td>
		<select name="params#paramIdx@.encode">
			<option value="GBK">gbk</option>
			<option value="UTF-8">utf-8</option>
		</select>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">负载攻击：</td>
	<td>
		<select name="params#paramIdx@.attack">
			<option value="true">是</option>
			<option value="false">否</option>
		</select>
	</td>
	</tr>
	<tr class="table_con">
	<td width="25%">类型：</td>
	<td>
		<select name="params#paramIdx@.type">
			<option value="URL">url</option>
		    <option value="PARAM">param</option>
			<option value="COOKIE">cookie</option>
			<option value="BODY">body</option>
		</select>
	</td>
	</tr>
	</tbody>
	</table>
</div>
</body>
</html>
